package apis

import (
	"dog-blog/app/post/resp"
	"dog-blog/base/logger"
	"net/http"

	"github.com/gin-gonic/gin/binding"

	"github.com/gin-gonic/gin"
)

type Api struct {
	Context *gin.Context
}

func (e *Api) SetContext(c *gin.Context) {
	e.Context = c
}

// GetLogger 获取日志
func (e Api) GetLogger() *logger.Logger {
	return logger.GetLogger()
}

func (e Api) Bind(d interface{}, bindings ...binding.Binding) error {
	var err error
	for i := range bindings {
		switch bindings[i] {
		case binding.JSON:
			err = e.Context.ShouldBindWith(d, binding.JSON)
		default:
			err = e.Context.ShouldBindUri(d)
		}
		if err != nil {
			return err
		}
	}
	return nil
}

// Default 通常成功数据处理
func (e Api) Default(cod int) {
	e.Custom(resp.BaseResp{ErrCode: cod, ErrDesc: resp.ErrDesc[cod]})
}

// Custom 兼容函数
func (e Api) Custom(resp interface{}) {
	e.Context.AbortWithStatusJSON(http.StatusOK, resp)
}
